package com.aquarium.ui;

import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.alarm.AlarmManager;
import com.aquarium.bt.BTManager;
import com.aquarium.sensor.SensorManager;

public class SensorValueDisplayActivity extends Activity implements Observer {

	private final static int HANDLE_UPDATE_SENSOR_VALUE = 1;
	private final static int HANDLE_UPDATE_SENSOR_ALARM_VALUE = 2;

	private SensorManager mSensorManager;
	private AlarmManager mAlarmManager;
	private BTManager mBTManager;
	private float mCurrentPhValue;
	private float mCurrentTemperatureValue;
	private float mCurrentOxygenValue;
	private float mCurrentConductivityValue;
	private float mCurrentWaterLevelValue;
	private HashMap<String, String> mSensorValueMap;

	private float mPhMin;
	private float mPhMax;
	private float mTemperatureMin;
	private float mTemperatureMax;
	private float mOxygenMin;
	private float mOxygenMax;
	private float mConductivityMin;
	private float mConductivityMax;
	private float mWaterLevelMin;
	private float mWaterLevelMax;

	private TextView mPHValueMinTextView;
	private TextView mPHValueCurrentTextView;
	private TextView mPHValueMaxTextView;
	private TextView mTempValueMinTextView;
	private TextView mTempValueCurrentTextView;
	private TextView mTempValueMaxTextView;
	private TextView mOxygenValueMinTextView;
	private TextView mOxygenValueCurrentTextView;
	private TextView mOxygenValueMaxTextView;
	private TextView mConductivityValueMinTextView;
	private TextView mConductivityValueCurrentTextView;
	private TextView mConductivityValueMaxTextView;
	private TextView mWaterLevelValueMinTextView;
	private TextView mWaterLevelValueCurrentTextView;
	private TextView mWaterLevelValueMaxTextView;

	private Button mHistoryDataButton;
	private Button mSettingButton;
	private Button mRefreshButton;

	private Handler mUiHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case HANDLE_UPDATE_SENSOR_VALUE:
				displayCurrentValue();
				break;
			case HANDLE_UPDATE_SENSOR_ALARM_VALUE:
				displayAlarmValue();
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.data_display_view);

		// TODO get latest value or default value and display them.
		// TODO we don't know when we got the value.

		mSensorManager = SensorManager.getInstance();
		mSensorManager.addObserver(this);
		mAlarmManager = AlarmManager.getInstance(this);
		mAlarmManager.addObserver(this);

		mBTManager = BTManager.getInstance();
		findViews();
		setListener();
	}

	private void updateAlarmValue() {
		mPhMax = mAlarmManager.getPhMax();
		mPhMin = mAlarmManager.getPhMin();
		mTemperatureMax = mAlarmManager.getTemperatureMax();
		mTemperatureMin = mAlarmManager.getTemperatureMin();
		mOxygenMax = mAlarmManager.getOxygenMax();
		mOxygenMin = mAlarmManager.getOxygenMin();
		mConductivityMax = mAlarmManager.getConductivityMax();
		mConductivityMin = mAlarmManager.getConductivityMin();
		mWaterLevelMax = mAlarmManager.getWaterLevelMax();
		mWaterLevelMin = mAlarmManager.getWaterLevelMin();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// test code
		mBTManager.sendSensorValueTest();
		mSensorManager.notifySensorValueTest();

		// when activity resume, we need update alarm value manually.
		updateAlarmValue();
		displayAlarmValue();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBTManager.stopSendSensorValueTest();
		mSensorManager.stopNotifySensorValueTest();
	}

	@Override
	public void update(Observable observable, Object data) {
		if (observable instanceof SensorManager) {
			Log.i("sensor activity", "update....");
			mSensorValueMap = (HashMap<String, String>) data;
			mCurrentPhValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.PH_VALUE_KEY));
			mCurrentTemperatureValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.TEMPERATURE_VALUE_KEY));
			mCurrentOxygenValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.OXYGEN_VALUE_KEY));
			mCurrentConductivityValue = Float.parseFloat(mSensorValueMap
					.get(SensorManager.CONDUCTIVITY_VALUE_KEY));
			mCurrentWaterLevelValue = Float.valueOf(mSensorValueMap
					.get(SensorManager.WATER_LEVEL_VALUE_KEY));

			mUiHandler.sendEmptyMessage(HANDLE_UPDATE_SENSOR_VALUE);
			// displayCurrentValue();
		} else if (observable instanceof AlarmManager) {
			HashMap<String, String> map = (HashMap<String, String>) data;
			// TODO find an more efficient way to get value.
			if (map.containsKey(AlarmManager.GET_PH_MIN_KEY)) {
				mPhMin = Float.parseFloat(map.get(AlarmManager.GET_PH_MIN_KEY));
			} else if (map.containsKey(AlarmManager.GET_PH_MAX_KEY)) {
				mPhMax = Float.parseFloat(map.get(AlarmManager.GET_PH_MAX_KEY));
			} else if (map.containsKey(AlarmManager.GET_PH_MAX_KEY)) {
				mTemperatureMin = Float.parseFloat(map
						.get(AlarmManager.GET_TEMPERATURE_MIN_KEY));
			} else if (map.containsKey(AlarmManager.GET_PH_MAX_KEY)) {
				mTemperatureMax = Float.parseFloat(map
						.get(AlarmManager.GET_TEMPERATURE_MAX_KEY));
			} else if (map.containsKey(AlarmManager.GET_OXYGEN_MIN_KEY)) {
				mOxygenMin = Float.parseFloat(map
						.get(AlarmManager.GET_OXYGEN_MIN_KEY));
			} else if (map.containsKey(AlarmManager.GET_OXYGEN_MAX_KEY)) {
				mOxygenMax = Float.parseFloat(map
						.get(AlarmManager.GET_OXYGEN_MAX_KEY));
			} else if (map.containsKey(AlarmManager.GET_CONDUCTIVITY_MIN_KEY)) {
				mConductivityMin = Float.parseFloat(map
						.get(AlarmManager.GET_CONDUCTIVITY_MIN_KEY));
			} else if (map.containsKey(AlarmManager.GET_CONDUCTIVITY_MAX_KEY)) {
				mConductivityMax = Float.parseFloat(map
						.get(AlarmManager.GET_CONDUCTIVITY_MAX_KEY));
			} else if (map.containsKey(AlarmManager.GET_WATER_LEVEL_MIN_KEY)) {
				mWaterLevelMin = Float.parseFloat(map
						.get(AlarmManager.GET_WATER_LEVEL_MIN_KEY));
			} else if (map.containsKey(AlarmManager.GET_WATER_LEVEL_MAX_KEY)) {
				mWaterLevelMax = Float.parseFloat(map
						.get(AlarmManager.GET_WATER_LEVEL_MAX_KEY));
			}

			mUiHandler.sendEmptyMessage(HANDLE_UPDATE_SENSOR_ALARM_VALUE);
		}
	}

	private void setListener() {

		mHistoryDataButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(SensorValueDisplayActivity.this,
						HistoryActivity.class);
				startActivity(intent);
			}
		});

		mSettingButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();

				intent.setClass(SensorValueDisplayActivity.this,
						AlarmSettingActivity.class);
				startActivity(intent);

			}
		});

		mRefreshButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO cause SensorManager to refresh data.

			}
		});
	}

	private void findViews() {
		mHistoryDataButton = (Button) findViewById(R.id.history_button);
		mSettingButton = (Button) findViewById(R.id.setting_button);
		mRefreshButton = (Button) findViewById(R.id.refresh_button);

		mPHValueMinTextView = (TextView) findViewById(R.id.ph_value_min);
		mPHValueCurrentTextView = (TextView) findViewById(R.id.ph_value_current);
		mPHValueMaxTextView = (TextView) findViewById(R.id.ph_value_max);
		mTempValueMinTextView = (TextView) findViewById(R.id.temperature_value_min);
		mTempValueCurrentTextView = (TextView) findViewById(R.id.temperature_value_current);
		mTempValueMaxTextView = (TextView) findViewById(R.id.temperature_value_max);

		mOxygenValueMinTextView = (TextView) findViewById(R.id.oxygen_value_min);
		mOxygenValueCurrentTextView = (TextView) findViewById(R.id.oxygen_value_current);
		mOxygenValueMaxTextView = (TextView) findViewById(R.id.oxygen_value_max);

		mConductivityValueMinTextView = (TextView) findViewById(R.id.conductivity_value_min);
		mConductivityValueCurrentTextView = (TextView) findViewById(R.id.conductivity_value_current);
		mConductivityValueMaxTextView = (TextView) findViewById(R.id.conductivity_value_max);

		mWaterLevelValueMinTextView = (TextView) findViewById(R.id.waterLevel_value_min);
		mWaterLevelValueCurrentTextView = (TextView) findViewById(R.id.waterLevel_value_current);
		mWaterLevelValueMaxTextView = (TextView) findViewById(R.id.waterLevel_value_max);
	}

	/**
	 * refresh current value on screen.
	 */
	private void displayCurrentValue() {
		mPHValueCurrentTextView.setText(Float.toString(mCurrentPhValue));
		mTempValueCurrentTextView.setText(Float
				.toString(mCurrentTemperatureValue));
		mOxygenValueCurrentTextView
				.setText(Float.toString(mCurrentOxygenValue));
		mConductivityValueCurrentTextView.setText(Float
				.toString(mCurrentConductivityValue));
		mWaterLevelValueCurrentTextView.setText(Float
				.toString(mCurrentWaterLevelValue));

		updateValueColor();
	}

	/**
	 * refresh alarm value on screen.
	 */
	private void displayAlarmValue() {
		mPHValueMinTextView.setText(Float.toString(mPhMin));
		mPHValueMaxTextView.setText(Float.toString(mPhMax));
		mTempValueMinTextView.setText(Float.toString(mTemperatureMin));
		mTempValueMaxTextView.setText(Float.toString(mTemperatureMax));
		mOxygenValueMinTextView.setText(Float.toString(mOxygenMin));
		mOxygenValueMaxTextView.setText(Float.toString(mOxygenMax));
		mConductivityValueMinTextView.setText(Float.toString(mConductivityMin));
		mConductivityValueMaxTextView.setText(Float.toString(mConductivityMax));
		mWaterLevelValueMinTextView.setText(Float.toString(mWaterLevelMin));
		mWaterLevelValueMaxTextView.setText(Float.toString(mWaterLevelMax));

		updateValueColor();
	}

	/**
	 * change the color of current value to indicate security.
	 */
	private void updateValueColor() {
		if (mCurrentPhValue > mPhMin && mCurrentPhValue < mPhMax) {
			mPHValueCurrentTextView.setTextColor(Color.GREEN);
		} else {
			mPHValueCurrentTextView.setTextColor(Color.RED);
		}

		if (mCurrentTemperatureValue > mTemperatureMin
				&& mCurrentTemperatureValue < mTemperatureMax) {
			mTempValueCurrentTextView.setTextColor(Color.GREEN);
		} else {
			mTempValueCurrentTextView.setTextColor(Color.RED);
		}

		if (mCurrentOxygenValue > mOxygenMin
				&& mCurrentOxygenValue < mOxygenMax) {
			mOxygenValueCurrentTextView.setTextColor(Color.GREEN);
		} else {
			mOxygenValueCurrentTextView.setTextColor(Color.RED);
		}

		if (mCurrentConductivityValue > mConductivityMin
				&& mCurrentConductivityValue < mConductivityMax) {
			mConductivityValueCurrentTextView.setTextColor(Color.GREEN);
		} else {
			mConductivityValueCurrentTextView.setTextColor(Color.RED);
		}

		if (mCurrentWaterLevelValue > mWaterLevelMin
				&& mCurrentWaterLevelValue < mWaterLevelMax) {
			mWaterLevelValueCurrentTextView.setTextColor(Color.GREEN);
		} else {
			mWaterLevelValueCurrentTextView.setTextColor(Color.RED);
		}
	}
}
